﻿// -------------------------------------------------------------------------
//    @FileName         :    NFSnsModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-29
//    @Email			:    445267987@qq.com
//    @Module           :    NFSnsModule
//
// -------------------------------------------------------------------------

#include "NFSnsModule.h"
#include <proto_svr_common.pb.h>
#include <proto_svr_sns.pb.h>
#include <NFComm/NFPluginModule/NFServerDefine.h>
#include "NFPlayer.h"
#include "NFPlayerMgr.h"

NFSnsModule::NFSnsModule(NFIPluginManager* p): NFMMODynamicModule(p)
{
}

NFSnsModule::~NFSnsModule()
{
}

int NFSnsModule::Awake()
{
    RegisterServerMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_LOGIN_RSP);
    RegisterServerMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_LOGOUT_RSP);
    return 0;
}

int NFSnsModule::Tick()
{
    return 0;
}

int NFSnsModule::OnDynamicPlugin()
{
    return 0;
}

int NFSnsModule::OnHandleServerMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case proto_ff::E_SG_LOGIN_RSP:
        {
            iRetCode = OnHandleLoginRsp(unLinkId, packet);
            break;
        }
        case proto_ff::E_SG_LOGOUT_RSP:
        {
            iRetCode = OnHandleLogoutRsp(unLinkId, packet);
            break;
        }
        default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Server MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }
    return iRetCode;
}

int NFSnsModule::OnExecute(uint32_t serverType, uint32_t eventId, uint32_t bySrcType, uint64_t srcId, const google::protobuf::Message* pMessage)
{
    return 0;
}

int NFSnsModule::OnHandleLoginRsp(uint64_t unLinkId, NFDataPackage& packet)
{
    proto_ff::Proto_SGLoginRsp rsp;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, rsp);

    NFPlayer* pPlayer = NFPlayerMgr::Instance()->GetPlayer(rsp.role_id());
    CHECK_NULL_WF(rsp.role_id(), pPlayer, "sns login rsp, role offline, role id:{}", rsp.role_id());

    LOG_INFO(rsp.role_id(), "Role Sns Login Rsp success, role id:{}", rsp.role_id());
    return 0;
}

int NFSnsModule::OnHandleLogoutRsp(uint64_t unLinkId, NFDataPackage& packet)
{
    proto_ff::Proto_SGLogoutRsp rsp;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, rsp);

    NFPlayer* pPlayer = NFPlayerMgr::Instance()->GetPlayer(rsp.role_id());
    if (pPlayer)
    {
        LOG_ERR(rsp.role_id(), -1, "sns logout rsp, but role data exist");
    }

    LOG_INFO(rsp.role_id(), "Role Sns Logout Rsp success, role id:{}", rsp.role_id());
    return 0;
}
